package com.zxc.demo2.factory;

import org.springframework.beans.factory.FactoryBean;

import java.sql.Connection;
import java.sql.DriverManager;

/**
  * @author: zxc
  * @return:
  * @time:  2021/5/17 17:22
  * @description:
  */
public class ConnectionFactoryBean implements FactoryBean<Connection> {

/**
  * @author: zxc
  * @return:
  * @time:  2021/5/17 17:24
  * @description: 用于返回创建的复杂对象的方法,后面spring容器会自动调用此方法获取对象实例
  */
    @Override
    public Connection getObject() throws Exception {
        //类加载
        Class.forName("com.mysql.jdbc.Driver");
        //创建链接
        Connection cos = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp?useUnicode=true&characterEncoding=utf8", "zxc", "123456");
        return cos;
    }

    /**
      * @author: zxc
      * @return:
      * @time:  2021/5/17 18:25
      * @description: 这个工厂类要返回的对象的数据类型
      */
    @Override
    public Class<?> getObjectType() {
        return Connection.class;
    }

    /**
      * @author: zxc
      * @return:
      * @time:  2021/5/17 18:26
      * @description: 设置创建出来的对象是否单例
     * false改为ture后为单例
      */
    @Override
    public boolean isSingleton() {
        return false;
    }
}
